package logic

import (
	"context"
	"fmt"
	"product-mall/models/mysql"

	"product-mall/productrpc/internal/svc"
	"product-mall/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type DelProductLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDelProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelProductLogic {
	return &DelProductLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DelProductLogic) DelProduct(in *productrpc.DelProductRequest) (*productrpc.DelProductResponse, error) {
	// todo: add your logic here and delete this line
	var product mysql.Product
	mysql.DB.Debug().Where("id = ?", in.Id).Find(&product)
	if product.UserId != in.UserId {
		return nil, fmt.Errorf("权限不足")
	}

	err := mysql.DB.Debug().Where("id = ?", in.Id).Delete(&product).Error
	if err != nil {
		return nil, fmt.Errorf("商品删除失败")
	}
	return &productrpc.DelProductResponse{}, nil
}
